﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataAccess;
using RM2012Service.Business;
using System.Data;

namespace RM2012Service.Controller
{
    public class TablesCtrl
    {
        TablesDAO dao;

        public TablesCtrl()
        {
            dao = new TablesDAO();
        }

        public TablesInfo[] GetAllTables()
        {
            TablesInfo[] list = null;
            try
            {
                DataTable dt = dao.GetAllTables();
                if (dt.Rows.Count > 0)
                {
                    list = new TablesInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new TablesInfo(dt.Rows[i]["TablesId"].ToString(),
                            dt.Rows[i]["TablesName"].ToString(),
                            (int)dt.Rows[i]["StateId"],
                            dt.Rows[i]["AreaId"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public TablesInfo[] GetTableByArea(string areaId)
        {
            TablesInfo[] list = null;
            try
            {
                DataTable dt = dao.GetTableByArea(areaId);
                if (dt.Rows.Count > 0)
                {
                    list = new TablesInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new TablesInfo(dt.Rows[i]["TablesId"].ToString(),
                            dt.Rows[i]["TablesName"].ToString(),
                            (int)dt.Rows[i]["StateId"],
                            dt.Rows[i]["AreaId"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public TablesInfo GetTableById(string tableId)
        {
            TablesInfo info = null;
            try
            {
                DataTable dt = dao.GetTableById(tableId);
                if (dt.Rows.Count > 0)
                {
                    info = new TablesInfo(dt.Rows[0]["TablesId"].ToString(),
                        dt.Rows[0]["TablesName"].ToString(),
                        (int)dt.Rows[0]["StateId"],
                        dt.Rows[0]["AreaId"].ToString(),
                        dt.Rows[0]["Remark"].ToString());
                }
            }
            catch
            {
                info = null;
            }
            return info;
        }

        public TablesInfo[] GetTableByState(int state)
        {
            TablesInfo[] list = null;
            try
            {
                DataTable dt = dao.GetTableByState(state);
                if (dt.Rows.Count > 0)
                {
                    list = new TablesInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new TablesInfo(dt.Rows[i]["TablesId"].ToString(),
                            dt.Rows[i]["TablesName"].ToString(),
                            (int)dt.Rows[i]["StateId"],
                            dt.Rows[i]["AreaId"].ToString(),
                            dt.Rows[i]["Remark"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public DataSet GetTableByStateDataSet(int state)
        {
            return dao.GetTableByStateDataSet(state);
        }

        public bool UpdateTableState(string tablesId, int stateId)
        {
            int result = dao.UpdateTableState(tablesId, stateId);
            if (result > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }
    }
}
